﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Web.Utility;
using BySoft.Server.Consts;
using System.Data;
using BySoft.Server.Business;
using BySoft.Web.Controls;

namespace BySoft.Web.Site.SysManage
{
    public partial class RoleMenber :  AuthBasePage
    {
        int m_Count = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData(0);
            }
        }

        private void BindData(int pageIndex)
        {

            DataTable dtData = SysRole.GetMemberForRole(txtId.Text.Trim(), txtName.Text.Trim(), pageIndex, pcData.PageSize, out m_Count);
            pcData.RecordCount = m_Count;
            if (dtData.Rows.Count > 0)
            {
                sgvMember.DataSource = dtData;
                sgvMember.DataBind();
                pcData.CurrentIndex = pageIndex;
                pcData.Visible = true;
            }
            else
            {
                UIHelper.BindEmptyGridView(sgvMember, dtData);
                pcData.Visible = false;
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            DataTable dtMember = new DataTable();
            dtMember.Columns.Add("ID");
            dtMember.Columns.Add("DISPLAY_ID");
            dtMember.Columns.Add("NAME");
            dtMember.Columns.Add("TempType");
            dtMember.AcceptChanges();
            foreach (GridViewRow gvr in sgvMember.Rows)
            {
                if (((CheckBox)gvr.FindControl("cbChoose")).Checked)
                {
                    DataRow dr = dtMember.NewRow();
                    dr[0] = sgvMember.DataKeys[gvr.RowIndex].Values["ID"].ToString();
                    dr[1] = Server.HtmlDecode(gvr.Cells[1].Text);
                    dr[2] = gvr.Cells[2].Text;
                    dr[3] = "2";
                    dtMember.Rows.Add(dr);
                }
            }
            dtMember.AcceptChanges();

            Session["RoleMember_Member"] = dtMember;
            if (dtMember.Rows.Count == 0)
            {
                UIHelper.Alert(this.Page, String.Format(Message.PleaseSelect, "成员"));
                return;
            }
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>CloseTopDialog();</script>");

        }

        #region 翻页
        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            BindData(e.PageIndex);
        }
        #endregion

    }
}
